home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1460.dms / var1460.adf / VSprites / Example3.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  12KB  |  432 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: VSprites                    Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program demonstrates how to animate several (!) VSprites. */
  21.  
  22.  
  23.  
  24. /* Since we use Intuition, include this file: */
  25. #include <intuition/intuition.h>
  26.  
  27. /* Include this file since you are using sprites: */
  28. #include <graphics/gels.h>
  29.  
  30.  
  31.  
  32. /* We will use 32 VSprites: */
  33. #define MAXVSPRITES 32
  34.  
  35. /* They will move one pixel each time: */
  36. #define SPEED 1
  37.  
  38.  
  39.  
  40. /* Declare the functions we are going to use: */
  41. void main();
  42. void clean_up();
  43.  
  44.  
  45.  
  46. struct IntuitionBase *IntuitionBase = NULL;
  47. /* We need to open the Graphics library since we are using sprites: */
  48. struct GfxBase *GfxBase = NULL;
  49.  
  50.  
  51. /* Declare a pointer to a Screen structure: */ 
  52. struct Screen *my_screen;
  53.  
  54. /* Declare and initialize your NewScreen structure: */
  55. struct NewScreen my_new_screen=
  56. {
  57.   0,            /* LeftEdge  Should always be 0. */
  58.   0,            /* TopEdge   Top of the display.*/
  59.   320,          /* Width     We are using a low-resolution screen. */
  60.   200,          /* Height    Non-Interlaced NTSC (American) display. */
  61.   2,            /* Depth     4 colours. */
  62.   0,            /* DetailPen Text should be drawn with colour reg. 0 */
  63.   1,            /* BlockPen  Blocks should be drawn with colour reg. 1 */
  64.   SPRITES,      /* ViewModes No special modes. (Low-res, Non-Interlaced) */
  65.   CUSTOMSCREEN, /* Type      Your own customized screen. */
  66.   NULL,         /* Font      Default font. */
  67.   "MY SCREEN",  /* Title     The screen' title. */
  68.   NULL,         /* Gadget    Must for the moment be NULL. */
  69.   NULL          /* BitMap    No special CustomBitMap. */
  70. };
  71.  
  72.  
  73.  
  74. /* Declare a pointer to a Window structure: */ 
  75. struct Window *my_window = NULL;
  76.  
  77. /* Declare and initialize your NewWindow structure: */
  78. struct NewWindow my_new_window=
  79. {
  80.   0,             /* LeftEdge    x position of the window. */
  81.   0,             /* TopEdge     y positio of the window. */
  82.   320,           /* Width       320 pixels wide. */
  83.   200,           /* Height      200 lines high. */
  84.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  85.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  86.   CLOSEWINDOW,   /* IDCMPFlags  The window will give us a message if the */
  87.                  /*             user has selected the Close window gad. */
  88.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  89.   WINDOWCLOSE|   /*             Close Gadget. */
  90.   WINDOWDRAG|    /*             Drag gadget. */
  91.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  92.   WINDOWSIZING|  /*             Sizing Gadget. */
  93.   ACTIVATE,      /*             The window should be Active when opened. */
  94.   NULL,          /* FirstGadget No Custom gadgets. */
  95.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  96.   "VSprites with no limitations!", /* Title */
  97.   NULL,          /* Screen      Will later be connected to a custom scr. */
  98.   NULL,          /* BitMap      No Custom BitMap. */
  99.   80,            /* MinWidth    We will not allow the window to become */
  100.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  101.   320,           /* MaxWidth    than 320 x 200. */
  102.   200,           /* MaxHeight */
  103.   CUSTOMSCREEN   /* Type        Connected to the Workbench Screen. */
  104. };
  105.  
  106.  
  107.  
  108. /* 1. Declare and initialize some sprite data: */
  109. /* (6 frames, 4 different images: 1 2 3 4 3 2) */
  110. UWORD chip ship_data[6][28]=
  111. {
  112.   {
  113.     0xFFF8, 0x0000,
  114.     0x0200, 0x0000,
  115.     0x877C, 0x0000,
  116.     0x8786, 0x027C,
  117.     0xBFBF, 0x02C6,
  118.     0xEDFF, 0x1AC2,
  119.     0xA57D, 0x1AFE,
  120.     0xBF19, 0x02FE,
  121.     0x8F12, 0x00FC,
  122.     0x04FC, 0x0000,
  123.     0x0809, 0x0000,
  124.     0x3FFE, 0x0000,
  125.   },
  126.   {
  127.     0x7FF0, 0x0000,
  128.     0x0200, 0x0000,
  129.     0x077C, 0x0000,
  130.     0x8786, 0x027C,
  131.     0xBFBF, 0x02C6,
  132.     0xEDFF, 0x1AC2,
  133.     0xA57D, 0x1AFE,
  134.     0xBF19, 0x02FE,
  135.     0x0F12, 0x00FC,
  136.     0x04FC, 0x0000,
  137.     0x0809, 0x0000,
  138.     0x3FFE, 0x0000,
  139.   },
  140.   {
  141.     0x3FE0, 0x0000,
  142.     0x0200, 0x0000,
  143.     0x877C, 0x0000,
  144.     0x8786, 0x027C,
  145.     0xBFBF, 0x02C6,
  146.     0xEDFF, 0x1AC2,
  147.     0xA57D, 0x1AFE,
  148.     0xBF19, 0x02FE,
  149.     0x8F12, 0x00FC,
  150.     0x04FC, 0x0000,
  151.     0x0809, 0x0000,
  152.     0x3FFE, 0x0000,
  153.   },
  154.   {
  155.     0x1FC0, 0x0000,
  156.     0x0200, 0x0000,
  157.     0x077C, 0x0000,
  158.     0x8786, 0x027C,
  159.     0xBFBF, 0x02C6,
  160.     0xEDFF, 0x1AC2,
  161.     0xA57D, 0x1AFE,
  162.     0xBF19, 0x02FE,
  163.     0x0F12, 0x00FC,
  164.     0x04FC, 0x0000,
  165.     0x0809, 0x0000,
  166.     0x3FFE, 0x0000,
  167.   },
  168.   {
  169.     0x3FE0, 0x0000,
  170.     0x0200, 0x0000,
  171.     0x877C, 0x0000,
  172.     0x8786, 0x027C,
  173.     0xBFBF, 0x02C6,
  174.     0xEDFF, 0x1AC2,
  175.     0xA57D, 0x1AFE,
  176.     0xBF19, 0x02FE,
  177.     0x8F12, 0x00FC,
  178.     0x04FC, 0x0000,
  179.     0x0809, 0x0000,
  180.     0x3FFE, 0x0000,
  181.   },
  182.   {
  183.     0x7FF0, 0x0000,
  184.     0x0200, 0x0000,
  185.     0x077C, 0x0000,
  186.     0x8786, 0x027C,
  187.     0xBFBF, 0x02C6,
  188.     0xEDFF, 0x1AC2,
  189.     0xA57D, 0x1AFE,
  190.     0xBF19, 0x02FE,
  191.     0x0F12, 0x00FC,
  192.     0x04FC, 0x0000,
  193.     0x0809, 0x0000,
  194.     0x3FFE, 0x0000,
  195.   }
  196. };
  197.  
  198.  
  199.  
  200. /* 2. Declare VSprite structures: */
  201. struct VSprite head, tail, vsprite[ MAXVSPRITES ];
  202.  
  203.  
  204. /* 3. Declare the VSprites' colour tables:     */
  205. WORD colour_table[ MAXVSPRITES ][ 3 ];
  206.  
  207.  
  208. /* 4. Declare a GelsInfo structure: */
  209. struct GelsInfo ginfo;
  210.  
  211.  
  212. /* This boolean variable will tell us if the VSprites are */
  213. /* in the list or not:                                    */
  214. BOOL vsprite_on = FALSE;
  215.  
  216.  
  217. /* This program will not open any console window if run from */
  218. /* Workbench, but we must therefore not print anything.      */
  219. /* Functions like printf() must therefore not be used.       */
  220. void _main()
  221. {
  222.   /* The GelsInfo structure needs the following arrays: */
  223.   WORD nextline[ 8 ];
  224.   WORD *lastcolor[ 8 ];
  225.  
  226.   /* Direction of the sprite: */
  227.   WORD x_direction[ MAXVSPRITES ];
  228.  
  229.   /* Boolean variable used for the while loop: */
  230.   BOOL close_me = FALSE;
  231.  
  232.   /* Declare a pointer to an IntuiMessage structure: */
  233.   struct IntuiMessage *my_message;
  234.  
  235.   UBYTE loop;      /* Used as counter in the for loop: */
  236.   UBYTE image = 0; /* Which image is used, 1-6.        */
  237.   UBYTE x = 0;     /* X and Y position.                */
  238.   UBYTE y = 0;
  239.  
  240.  
  241.  
  242.   /* Open the Intuition Library: */
  243.   IntuitionBase = (struct IntuitionBase *)
  244.     OpenLibrary( "intuition.library", 0 );
  245.   
  246.   if( IntuitionBase == NULL )
  247.     clean_up(); /* Could NOT open the Intuition Library! */
  248.  
  249.  
  250.  
  251.   /* Since we are using sprites we need to open the Graphics Library: */
  252.   /* Open the Graphics Library: */
  253.   GfxBase = (struct GfxBase *)
  254.     OpenLibrary( "graphics.library", 0);
  255.  
  256.   if( GfxBase == NULL )
  257.     clean_up(); /* Could NOT open the Graphics Library! */
  258.  
  259.  
  260.  
  261.   /* We will now try to open the screen: */
  262.   my_screen = (struct Screen *) OpenScreen( &my_new_screen );
  263.  
  264.   /* Have we opened the screen succesfully? */
  265.   if(my_screen == NULL)
  266.     clean_up();
  267.  
  268.  
  269.   my_new_window.Screen = my_screen;
  270.  
  271.  
  272.   /* We will now try to open the window: */
  273.   my_window = (struct Window *) OpenWindow( &my_new_window );
  274.   
  275.   /* Have we opened the window succesfully? */
  276.   if(my_window == NULL)
  277.     clean_up(); /* Could NOT open the Window! */
  278.  
  279.  
  280.  
  281.  
  282.   /* 6. Initialize the GelsInfo structure: */
  283.  
  284.   /* All sprites may be used to draw the VSprites: */
  285.   /* ( 11111111 = 0xFF )                           */
  286.   ginfo.sprRsrvd = 0xFF;
  287.   /* If we do not exclude the first two sprites, the mouse */
  288.   /* pointer's colours may be affected.                    */
  289.  
  290.  
  291.   /* Give the GelsInfo structure some memory: */
  292.   ginfo.nextLine = nextline;
  293.   ginfo.lastColor = lastcolor;
  294.  
  295.  
  296.   /* Give the Rastport a pointer to the GelsInfo structure: */
  297.   my_window->RPort->GelsInfo = &ginfo;
  298.  
  299.   
  300.   /* Give the GelsInfo structure to the system: */
  301.   InitGels( &head, &tail, &ginfo );
  302.  
  303.  
  304.  
  305.   /* 7. Initialize the VSprite structures: */
  306.   for( loop = 0; loop < MAXVSPRITES; loop++ )
  307.   {
  308.     /* Set the VSprite's colours: */
  309.     colour_table[ loop ][ 0 ] = 0x0000; /* Black */
  310.     colour_table[ loop ][ 1 ] = 0x0080; /* Dark green */
  311.     colour_table[ loop ][ 2 ] = 0x00D0; /* Green */
  312.     
  313.     /* Set the speed and horizontal direction of the VSprite: */
  314.     x_direction[ loop ] = SPEED;
  315.  
  316.     vsprite[ loop ].Flags = VSPRITE; /* It is a VSprite.    */
  317.     vsprite[ loop ].X = 10 + 20 * x; /* X position.         */
  318.     vsprite[ loop ].Y = 30 + 20 * y; /* Y position.         */
  319.     vsprite[ loop ].Height = 12;     /* 16 lines tall.      */
  320.     vsprite[ loop ].Width = 2;       /* 2 words wide.       */
  321.     vsprite[ loop ].Depth = 2;       /* 2 bitpl, 4 colours. */
  322.  
  323.     /* Pointer to the sprite data: */
  324.     vsprite[ loop ].ImageData = ship_data[ image ];
  325.  
  326.     /* Pointer to the colour table: */
  327.     vsprite[ loop ].SprColors = colour_table[ loop ];
  328.  
  329.  
  330.     /* 8. Add the VSprites to the VSprite list: */
  331.     AddVSprite( &vsprite[ loop ], my_window->RPort );
  332.  
  333.  
  334.     /* Position of the VSprites: */
  335.     y++;
  336.     if( y > 7 )
  337.     {
  338.       y = 0;
  339.       x++;
  340.     }
  341.   }
  342.   
  343.   
  344.   /* The VSprites are in the list. */
  345.   vsprite_on = TRUE;
  346.  
  347.  
  348.   /* Stay in the while loop until the user has selected the Close window */
  349.   /* gadget: */
  350.   while( close_me == FALSE )
  351.   {
  352.     /* Stay in the while loop as long as we can collect messages: */
  353.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  354.     {
  355.       if( my_message->Class == CLOSEWINDOW)
  356.         close_me=TRUE;
  357.  
  358.       ReplyMsg( my_message );
  359.     }
  360.  
  361.  
  362.     /* Affect all VSprites: */
  363.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  364.     {
  365.       /* Change the x position of the VSprite: */
  366.       vsprite[ loop ].X += x_direction[ loop ];
  367.  
  368.  
  369.       /* Check that the sprite does not move outside the screen: */
  370.       if(vsprite[ loop ].X > 300)
  371.         x_direction[ loop ] = -SPEED;
  372.  
  373.       if(vsprite[ loop ].X < 0)
  374.         x_direction[ loop ] = SPEED;
  375.  
  376.  
  377.       /* Change the image of the VSprite: */
  378.       vsprite[ loop ].ImageData = ship_data[ image ];
  379.     }
  380.     
  381.  
  382.     /* Image counter: */
  383.     image++;
  384.     if( image > 5 )
  385.       image = 0;
  386.   
  387.  
  388.     /* 9. Sort the Gels list: */
  389.     SortGList( my_window->RPort );
  390.  
  391.     /* 10. Draw the Gels list: */
  392.     DrawGList( my_window->RPort, &(my_screen->ViewPort) );
  393.  
  394.     /* 11. Set the Copper and redraw the display: */
  395.     MakeScreen( my_screen );
  396.     RethinkDisplay();    
  397.   }
  398.  
  399.  
  400.  
  401.   /* Free all allocated memory: (Close the window, libraries etc) */
  402.   clean_up();
  403.  
  404.   /* THE END */
  405. }
  406.  
  407.  
  408.  
  409. /* This function frees all allocated memory. */
  410. void clean_up()
  411. {
  412.   UBYTE loop;
  413.   
  414.   if( vsprite_on )
  415.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  416.       RemVSprite( &vsprite[ loop ] );
  417.  
  418.   if( my_window )
  419.     CloseWindow( my_window );
  420.   
  421.   if(my_screen )
  422.     CloseScreen( my_screen );
  423.  
  424.   if( GfxBase )
  425.     CloseLibrary( GfxBase );
  426.  
  427.   if( IntuitionBase )
  428.     CloseLibrary( IntuitionBase );
  429.  
  430.   exit();
  431. }
  432.